
function changeWarehouse(index)
{
  var flag = true;
  var warehouse = eval("frm.warehouseid" + index);
  var qty=eval("frm.orderqty" + index);
  if(flag && qty.value-warehouse.atpqty>0 || qty.value - warehouse.options[warehouse.selectedIndex].atpqty >0 )
  { 
    if(<%=JspHelper.filterNull( atpmsglevel )%> == 1)
    {
      if(<%=JspHelper.filterNull( !isShOrder )%> && !confirm(warehouse.atpmsg))
        flag = false;
    }else if(<%=JspHelper.filterNull(isShOrder)%> || <%=JspHelper.filterNull(atpmsglevel)%> == 2)
    {
      alert(warehouse.atpmsg);
      flag = false;
    }
  }
  if(!flag)
  {
    qty.value = "0";
    qty.tempvalue ="0";
    document.body.focus();
    qty.select();
    qty.focus();
	  if(!sumOne(index)) return;
	  if(!isEmpty(index))
	  if(!sumAll()) return;
    formatNumberForOrder(index);
  }
  var linestatus = eval("frm.linestatus" + index);
  linestatus.value = "10";
}
function changeQty(index)
{
  var qty=eval("frm.orderqty" + index);
  if(!checkQty(qty,index))
  {
    qty.value = qty.tempvalue;
    return;
  }
  qty.value = numberRound(numberToString(qty.value),qty.dataFormat);
  qty.tempvalue = qty.value;
  var linestatus = eval("frm.linestatus" + index);
  if(isEmpty(qty.originalqty) || qty.value - qty.originalqty != 0)
	  linestatus.value = "10";
	else
	  linestatus.value = linestatus.originallinestatus;
	if(!sumOne(index)) return;
	if(!isEmpty(index))
	if(!sumAll()) return;
  formatNumberForOrder(index);
}
function changeNetPrice(index)
{
  var netprice=eval("frm.linenetprice" + index);
  if(!checkNetPrice(netprice))
  {
    netprice.value = netprice.tempvalue;
    return;
  }
  netprice.value = numberRound(numberToString(netprice.value),netprice.dataFormat);
  netprice.tempvalue = netprice.value;
  calculateLinePrice(index,"linenetprice");
	if(!sumOne(index)) return;
	if(!isEmpty(index))
	if(!sumAll()) return;
  formatNumberForOrder(index);
}
function changeDiscount(index)
{
  var discountDis=eval("frm.discountDis" + index);
  var discount=eval("frm.discount" + index);
  if(discountDis.value=='')
    discountDis.value = 0;
  if(!checkDiscount(discountDis))
  {
    discountDis.value = discountDis.tempvalue;                             
    return;
  }

  var bDiscountDis = new BigDecimal("0");
  if (!isEmpty(discountDis.value))
     bDiscountDis = new BigDecimal(""+discountDis.value);

  discount.value = numberRound(numberToString(bDiscountDis.divide(BigDecimal.prototype.HUNDRED,10,BigDecimal.prototype.ROUND_HALF_UP)),discount.dataFormat);
  discount.tempvalue = discount.value;
  calculateLinePrice(index,"discount");
	if(!sumOne(index)) return;
	if(!isEmpty(index))
	if(!sumAll()) return;
  formatNumberForOrder(index);
}
function changeDiscountAmt(index)
{
  var discountamt=eval("frm.discountamt" + index);
  if(discountamt.value=='')
    discountamt.value = 0;
  var unitprice=eval("frm.unitprice" + index);
  if(!checkDiscountAmt(discountamt,unitprice))
  {
    discountamt.value = discountamt.tempvalue;
    return;
  }
  unitprice.value = numberRound(numberToString(unitprice.value),unitprice.dataFormat);
  discountamt.value = numberRound(numberToString(discountamt.value),discountamt.dataFormat);
  discountamt.tempvalue = discountamt.value;
  calculateLinePrice(index,"discountamt");
	if(!sumOne(index)) return;
	if(!isEmpty(index))
	if(!sumAll()) return;
  formatNumberForOrder(index);
}
function changeTotalDiscount()
{
  var totaldiscount=eval("frm.totaldiscount");
  if(!checkTotalDiscount())
  {
    totaldiscount.value = totaldiscount.tempvalue;
    return;
  }
  totaldiscount.value = numberRound(numberToString(totaldiscount.value),totaldiscount.dataFormat);
  totaldiscount.tempvalue = totaldiscount.value;
  if(!sumAll("totaldiscount"))return;
  formatTotalNumberForOrder();
}
function changeTotalDiscountAmt()
{
  var totaldiscountamt=eval("frm.totaldiscountamt");
  if(!checkTotalDiscountAmt())
  {
    totaldiscountamt.value = totaldiscountamt.tempvalue;
    return;
  }
  totaldiscountamt.value = numberRound(numberToString(totaldiscountamt.value),totaldiscountamt.dataFormat);
  totaldiscountamt.tempvalue = totaldiscountamt.value;
  if(!sumAll("totaldiscountamt"))return;
  formatTotalNumberForOrder();
}

function changeLineType(index)
{
  var invattricode = eval("frm.invattricode" + index);
  var linetype = eval("frm.linetype" + index);
  if( (linetype.value=="3")&&  invattricode.value=="1" )
  {
    alert(replaceString("<%= message.getMessageDesc("INVATTRICODE_MUST_IS_NULL") %>","%s",'<%= label.getLabelName("L_OrderQty",false) %>',"0"));
    frm.linetype.value=1;
    return;
  }
  <%if(isServiceOrder){%>
var linenetprice = eval("frm.linenetprice" + index);
  if(linetype.value=="4" || linetype.value=="5" || linetype.value=="6")
  {
  linenetprice.value="0";
   changeProtectValue('','linenetprice');
  }
  <%}%>
  
}

function checkQty(qty,index)
{
  var flag = true;
  var warehouse = eval("frm.warehouseid" + index);
  var umcode = eval("frm.umcode" + index);
  var invattricode = eval("frm.invattricode" + index);
  var linetype = eval("frm.linetype" + index);

  //alert(linetype);
  if(isEmpty(qty.value))
  {
    alert("<%= message.getMessageDesc("VALUE_NOT_NULL",label.getLabelName("L_OrderQty",false)) %>");
    flag = false;
  }
  if(flag && !isNumberic(qty.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_NUMBER",label.getLabelName("L_OrderQty",false)) %>");
    flag = false;
  }
  if(flag && (linetype.value!="3")&& Number(qty.value) <= 0)
  {
    alert(replaceString("<%= message.getMessageDesc("VALUE_MUST_MORETHAN") %>","%s",'<%= label.getLabelName("L_OrderQty",false) %>',"0"));
    flag = false;
  }
   if( flag && (linetype.value=="3")&&  invattricode.value=="1" )
  {
    alert(replaceString("<%= message.getMessageDesc("INVATTRICODE_MUST_IS_NULL") %>","%s",'<%= label.getLabelName("L_OrderQty",false) %>',"0"));
    flag = false;
  }
  if( !isEmpty(warehouse) && (invattricode.value != "4"))
  {
    if(flag && warehouse.atpqty!=null && qty.value*umcode.basefactor-warehouse.atpqty>0 || qty.value*umcode.basefactor - warehouse.options[warehouse.selectedIndex].atpqty >0 )
    {
       if(<%=JspHelper.filterNull( !isShOrder )%> && <%=JspHelper.filterNull( atpmsglevel )%> == 1 )
      {
        if(!confirm(warehouse.atpmsg))
        {
          qty.value="";
          flag = false;
        }else
        {
          frm.checkqty.value = "N";
        }
      }else if(<%=JspHelper.filterNull(isShOrder)%> || <%=JspHelper.filterNull(atpmsglevel)%> == 2 )
      {
        alert(warehouse.atpmsg);
        flag = false;
      }
    }
  }
  if(flag && (linetype.value!="3") && numberRound(""+qty.value*umcode.buyerfactor,"<%=JspHelper.filterNull( qtyformat )%>") <= 0 )
  {
    alert("<%= message.getMessageDesc("LITTLE_THAN_BUYER_UNIT_QTY")  %>");
    flag = false;
  }
  if(flag)
  {
      qty.style.color = "";
  }else{
    document.body.focus();
    qty.select();
    qty.focus();
  }
  //checkNumber(frm);
  return flag;
}

function checkNetPrice(price)
{
  var flag = true;
  if(flag && !isNumberic(price.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_NUMBER",label.getLabelName("L_NetPrice",false)) %>");
    flag = false;
  }
  if(flag && Number(price.value) < 0)
  {
    alert(replaceString("<%= message.getMessageDesc("VALUE_MUST_MORETHAN") %>","%s",'<%= label.getLabelName("L_NetPrice",false) %>',"0"));
    flag = false;
  }
  if(flag && Number(price.value) == 0 && !confirm("<%= message.getMessageDesc("ZERO_VALUE_CONTINUE",label.getLabelName("L_NetPrice",false)) %>"))
    flag = false;
  if(!flag)
  {
    document.body.focus();
    price.select();
    price.focus();
  }
  //checkNumber(frm);
  return flag;
}
function checkDiscount(discount)
{
  var flag = true;
  if(!isNumberic(discount.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_NUMBER",label.getLabelName("L_DiscountRate",false)) %>");
    flag = false;
  }
  if(flag && (discount.value > 100 || discount.value < 0))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BETWEEN",label.getLabelName("L_DiscountRate",false),"0%","100%") %>");
    flag = false;
  }
  if(flag && discount.value - 100 == 0)
  {
    if(!confirm("<%= message.getMessageDesc("ZERO_VALUE_CONTINUE",label.getLabelName("L_NetPrice",false)) %>"))
      flag = false;
  }
  if(!flag)
  {
    document.body.focus();
    discount.select();
    discount.focus();
  }
  //checkNumber(frm);
  return flag;
}
function checkDiscountAmt(discountamt,unitprice)
{
  var flag = true;
  if(!isNumberic(discountamt.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_NUMBER",label.getLabelName("L_Discount",false)) %>");
    flag = false;
  }
  if(flag && discountamt.value - unitprice.value > 0)
  {
    alert("<%= message.getMessageDesc("VALUE_NOT_MORETHAN",label.getLabelName("L_Discount",false),label.getLabelName("L_Price",false)) %>");
    flag = false;
  }
  if(flag && discountamt.value - unitprice.value == 0)
  {
    if(!confirm("<%= message.getMessageDesc("ZERO_VALUE_CONTINUE",label.getLabelName("L_NetPrice",false)) %>"))
      flag = false;
  }
  if(!flag)
  {
    document.body.focus();
    discountamt.select();
    discountamt.focus();
  }
  //checkNumber(frm);
  return flag;
}
function checkTotalDiscount()
{
  var flag = true;
  if(!isNumberic(frm.totaldiscount.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_NUMBER",label.getLabelName("L_DiscountRate",false)) %>");
    var flag = false;
  }
  if(flag && (frm.totaldiscount.value > 100 || frm.totaldiscount.value < 0))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BETWEEN",label.getLabelName("L_DiscountRate",false),"0%","100%") %>");
    var flag = false;
  }
  if(!flag)
  {
    document.body.focus();
    frm.totaldiscount.select();
    frm.totaldiscount.focus();
  }
  //checkNumber(frm);
  return flag;
}

function checkTotalDiscountAmt()
{
  var flag = true;
  if(!isNumberic(frm.totaldiscountamt.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_NUMBER",label.getLabelName("L_Discount",false)) %>");
    flag = false;
  }
  if(flag && frm.totaldiscountamt.value - frm.subtotalamt.value > 0)
  {
    alert("<%= message.getMessageDesc("VALUE_NOT_MORETHAN",label.getLabelName("L_DiscountAmt",false),label.getLabelName("L_Subtotal",false)) %>");
    flag = false;
  }
  if(!flag)
  {
    document.body.focus();
    frm.totaldiscountamt.select();
    frm.totaldiscountamt.focus();
  }
  //checkNumber(frm);
  return flag;
}

function calculateLinePrice(index,opt)
{
  var unitprice = eval("frm.unitprice" + index);
  var discount = eval("frm.discount" + index);
  var discountDis=eval("frm.discountDis" + index);
  var discountamt = eval("frm.discountamt" + index);
  var linenetprice = eval("frm.linenetprice" + index);
  var pretaxprice = eval("frm.pretaxprice" + index);
  var taxrate = eval("frm.taxrate" + index);

  var locallinenetprice = eval("frm.locallinenetprice" + index);
  var objzctrate=eval("frm.zctrate");


  var bUnitprice = new BigDecimal("0");
  if(!isEmpty(unitprice.value))
     bUnitprice = new BigDecimal(""+unitprice.value);
  var bDiscount = new BigDecimal("0");
  if (!isEmpty(discount.value))
     bDiscount = new BigDecimal(""+discount.value);

  if(opt == "discount"){
    discountamt.value = " ";
    discountamt.tempvalue = "";
    linenetprice.value = numberRound(numberToString(bUnitprice.multiply(BigDecimal.prototype.ONE.subtract(bDiscount))),linenetprice.dataFormat);
    linenetprice.tempvalue = linenetprice.value;
  }else if(opt == "discountamt"){
    discount.value = " ";
    discount.tempvalue = "";
    discountDis.value = "";
    discountDis.tempvalue = discountDis.value;
    linenetprice.value = numberRound(numberToString(unitprice.value - discountamt.value),linenetprice.dataFormat);
    linenetprice.tempvalue = linenetprice.value;
  }else if(opt == "linenetprice"){
    discount.value = " ";
    discount.tempvalue = "";
    discountDis.value = "";
    discountDis.tempvalue = discountDis.value;
    discountamt.value = unitprice.value - linenetprice.value;
    discountamt.tempvalue = discountamt.value;
  }

  var bLinenetprice = new BigDecimal("0");
  if (!isEmpty(linenetprice.value))
      bLinenetprice = new BigDecimal(""+linenetprice.value);
  var bZctrate = new BigDecimal("0");
  if (!isEmpty(objzctrate.value))
      bZctrate = new BigDecimal(""+objzctrate.value);
  locallinenetprice.value= numberRound(numberToString(bLinenetprice.multiply(bZctrate)),locallinenetprice.dataFormat);

  var bLocallinenetprice = new BigDecimal("0");
  if (!isEmpty(locallinenetprice.value))
      bLocallinenetprice = new BigDecimal(""+locallinenetprice.value);
  var bTaxrate = new BigDecimal("0");
  if (!isEmpty(taxrate.value))
      bTaxrate = new BigDecimal(""+taxrate.value);
  pretaxprice.value = numberRound(numberToString(bLocallinenetprice.divide(BigDecimal.prototype.ONE.add(bTaxrate),10,BigDecimal.prototype.ROUND_HALF_UP)),pretaxprice.dataFormat);

  return true;
}

function sumOne(index)
{
  //checkNumber(frm);
  var unitprice = eval("frm.unitprice" + index);
  var discount = eval("frm.discount" + index);
  var discountamt = eval("frm.discountamt" + index);
  var linenetprice = eval("frm.linenetprice" + index);
  var locallinenetprice = eval("frm.locallinenetprice" + index);
  var orderqty = eval("frm.orderqty" + index);
  var amount = eval("frm.amount" + index);
  var taxrate = eval("frm.taxrate" + index);
  var pretaxprice = eval("frm.pretaxprice" + index);
  var taxamount = eval("frm.taxamount" + index);
  var pretaxamount = eval("frm.pretaxamount" + index);
  var baseqty = eval("frm.baseqty" + index);
  var umcode = eval("frm.umcode" + index);
  var zctrate = eval("frm.zctrate");
  var zctamount = eval("frm.zctamount" + index);

  var bOrderqty = new BigDecimal("0");
  if (!isEmpty(orderqty.value))
     bOrderqty = new BigDecimal(""+orderqty.value);
  var bLinenetprice = new BigDecimal("0");
  if (!isEmpty(linenetprice.value))
     bLinenetprice = new BigDecimal(""+linenetprice.value);
  var bLocallinenetprice = new BigDecimal("0");
  if (!isEmpty(locallinenetprice.value))
      bLocallinenetprice = new BigDecimal(""+locallinenetprice.value);
  var bBasefactor = new BigDecimal("0");
  if (!isEmpty(umcode.basefactor))
      bBasefactor = new BigDecimal(""+umcode.basefactor);
  var bTaxrate = new BigDecimal("0");
  if (!isEmpty(taxrate.value))
      bTaxrate = new BigDecimal(""+taxrate.value);

  amount.value = numberRound(numberToString(bOrderqty.multiply(bLinenetprice)),amount.dataFormat);
  pretaxprice.value = numberRound(numberToString(bLocallinenetprice.divide(BigDecimal.prototype.ONE.add(bTaxrate),10,BigDecimal.prototype.ROUND_HALF_UP)),pretaxprice.dataFormat);
  var bPretaxprice = new BigDecimal("0");
  if(!isEmpty(pretaxprice.value))
      bPretaxprice = new BigDecimal(""+pretaxprice.value);
  pretaxamount.value = numberRound(numberToString(bOrderqty.multiply(bPretaxprice)),pretaxamount.dataFormat);
  zctamount.value = numberRound(numberToString(bOrderqty.multiply(bLocallinenetprice)),zctamount.dataFormat);
  taxamount.value = zctamount.value - pretaxamount.value;
  baseqty.value = numberRound(numberToString(bOrderqty.multiply(bBasefactor)),baseqty.dataFormat);

  formatNumberForOrder(index);
  //checkNumber(frm);
  return true;
}

function sumAll(val)
{
  var totala = 0;
  var totalt = 0;
//  with(frm)
//  {
//    for(var i = 0;i < numrows.value;i++)
//    {
//      var oneamt = eval("amount" + i);
//      var onetax = eval("taxamount" + i);
//      totala += Number(oneamt.value);
//      totalt += Number(onetax.value);
//    }
//  }
//  var totalamount = eval("frm.totalamount");
//  var totaltaxamt = eval("frm.totaltaxamount");
//  var subtotalamt = eval("frm.subtotalamt");
  var totaldiscount = eval("frm.totaldiscount");
  var totaldiscountamt = eval("frm.totaldiscountamt");
//  totaltaxamt.value = totalt;
//  subtotalamt.value = totala;
  if(val == "totaldiscount")
  {
    totaldiscountamt.value = " ";
//    totalamount.value = subtotalamt.value*(1-totaldiscount.value/100);
  }
  if(val == "totaldiscountamt")
  {
    totaldiscount.value = " ";
//    totalamount.value = subtotalamt.value - totaldiscountamt.value;
  }
//  if(isEmpty(val))
//  {
//    if(isEmpty(frm.totaldiscount.value))
//      frm.totalamount.value = frm.subtotalamt.value - frm.totaldiscountamt.value;
//    else
//      frm.totalamount.value = frm.subtotalamt.value*(1 - frm.totaldiscount.value/100);
//  }
  //checkNumber(frm);
  if(Number(totaldiscount.value) == 0 && Number(totaldiscountamt.value) == 0)
    frm.totaldiscountopt.value = "nodiscount";
  else if(Number(totaldiscount.value) == 0)
    frm.totaldiscountopt.value = "totaldiscountamt";
  else
    frm.totaldiscountopt.value = "totaldiscount";
    if (totaldiscountamt.value=="")
       totaldiscountamt.value=" ";
  return true;
}
function reSumAll()
{
  return true;

  var totalamt = 0;
  var ttaxamt = 0;
  with(frm)
  {
    for(var index=0;index< <%=JspHelper.filterNull( orderDetails.size() )%>;index++)
    {
      if(<%=JspHelper.filterNull( orderDetails.size() )%> == 1 && detailids.checked == true)
        break;
      else if(detailids[index].checked == true)
        continue;
      totalamt += Number(detailids[index].amount);
      ttaxamt += Number(detailids[index].taxamount);
    }
    subtotalamt.value = totalamt;
    totaldiscount.value = totaldiscount.realtotaldiscount;
    totaldiscountamt.value = totaldiscountamt.realtotaldiscountamt;
    if(subtotalamt.value - totaldiscountamt.value < 0 )
      totaldiscountamt.value = 0.0;
    if(totaldiscount.value)
      totalamount.value = subtotalamt.value - totaldiscountamt.value;
    else
    {
      var bSubtotalamt = new BigDecimal("0");
      if (!isEmpty(subtotalamt.value))
         bSubtotalamt = new BigDecimal(""+subtotalamt.value);
      var bTotaldiscount = new BigDecimal("0");
      if (!isEmpty(totaldiscount.value))
          bTotaldiscount = new BigDecimal(""+totaldiscount.value);

      totalamount.value = bSubtotalamt.multiply(BigDecimal.prototype.ONE.subtract( bTotaldiscount.divide(BigDecimal.prototype.HUNDRED,10,BigDecimal.prototype.ROUND_HALF_UP)));
    }
    totaltaxamount.value = ttaxamt;
  }

}

function addDiscountValue()
{
  var tempvalue = "";
  if(frm.totaldiscount.value != "")
  {
    var bTotaldiscount = new BigDecimal("0");
    if (!isEmpty(frm.totaldiscount.value))
      bTotaldiscount = new BigDecimal(""+frm.totaldiscount.value);
    tempvalue = bTotaldiscount.divide(BigDecimal.prototype.HUNDRED,10,BigDecimal.prototype.ROUND_HALF_UP);
  }else  {
  frm.totaldiscount.value=" ";
  }
  if(tempvalue != "")
  {
    frm.totaldiscount.name = "temp";
    addElementToForm(frm,"totaldiscount",tempvalue,false,"hidden");
  }
//  tempvalue = "";
//  if(frm.discount.value != "")
//    tempvalue = frm.discount.value;
//  if(tempvalue != "")
//  {
//    frm.discount.name = "temp";
//    addElementToForm(frm,"discount",tempvalue,false,"hidden");
//  }
//  for(var i = 0;i < frm.numrows.value;i++)
//  {
//    var discountx = eval("frm.discount" + i);
//    tempvalue = "";
//    if(discountx.value != "")
//      tempvalue = discountx.value/100;
//    if(tempvalue != "")
//    {
//      discountx.name = "temp";
//      addElementToForm(frm,"discount" + i,tempvalue,false,"hidden");
//    }
//  }
}
function checkAllQty()
{
  for(var i = 0;i < frm.numrows.value;i++)
  {
    var orderqty = eval("frm.orderqty" + i);
    if(Number(orderqty.value) == 0)
    {
      alert(replaceString("<%= message.getMessageDesc("VALUE_MUST_MORETHAN") %>","%s",'<%= label.getLabelName("L_OrderQty",false) %>',"0"));
      document.body.focus();
      orderqty.select();
      orderqty.focus();
      return false;
    }
    if(orderqty.style.color = "red")
      if(!checkQty(orderqty,i))
        return false;
  }
  return true;
}
function changeItemCode()
{
  if(isEmpty(frm.<%=JspHelper.filterNull(itemcodename)%>.value))
  {
    destroy();
    return;
  }
  frm.selleritemid.value="";
  frm.buyeritemid.value="";
  frm.shipfromitemid.value="";
  frm.itemid.value="";
  addDiscountValue();
  frm.pagename.value="salesorderdetailsummary";
  frm.actionname.value="changecode";
  frm.target="_self";
  isRunning = true;
  frm.submit();
  return;
}

function page_add()
{
<%if(isServiceOrder){%>

  if(frm.linetype.value=="4" || frm.linetype.value=="5" || frm.linetype.value=="6")
  {
   if(Number(frm.linenetprice.value)!=0)
   {
   alert("<%= message.getMessageDesc("VALUE_MUST_BE_ZERO",label.getLabelName("L_NetPrice",false)) %>");
    
   		return;
   }
  }
  <%}%>

  if(isEmpty(frm.<%=JspHelper.filterNull(itemcodename)%>.value))
  {
    alert("<%= message.getMessageDesc("MUST_SELECT_FIRST",label.getLabelName("L_Item",false)) %>");
    frm.<%=JspHelper.filterNull(itemcodename)%>.focus();
    return false;
  }
  if(!checkQty(frm.orderqty,""))
    return false;
  if(Number(frm.orderqty.value) == 0)
  {
    alert(replaceString("<%= message.getMessageDesc("VALUE_MUST_MORETHAN") %>","%s",'<%= label.getLabelName("L_OrderQty",false) %>',"0"));
    document.body.focus();
    frm.orderqty.select();
    frm.orderqty.focus();
    return false;
  }
  if(!sumOne(""))
    return false;
  frm.subtotalamt.value = Number(frm.subtotalamt.realsubtotalamt) + Number(frm.amount.value);
  frm.totaldiscount.value = frm.totaldiscount.realtotaldiscount;
  frm.totaldiscountamt.value = frm.totaldiscountamt.realtotaldiscountamt;
  frm.totaltaxamount.value = Number(frm.totaltaxamount.realtotaltaxamount) + Number(frm.taxamount.value);
  if(isEmpty(frm.totaldiscount.value))
    frm.totalamount.value = frm.subtotalamt.value - frm.totaldiscountamt.value;
  else
  {
    var bSubtotalamt = new BigDecimal("0");
    if (!isEmpty(frm.subtotalamt.value))
        bSubtotalamt = new BigDecimal(""+frm.subtotalamt.value);
    var bTotaldiscount = new BigDecimal("0");
    if (!isEmpty(frm.totaldiscount.value))
        bTotaldiscount = new BigDecimal(""+frm.totaldiscount.value);
    frm.totalamount.value = bSubtotalamt.multiply(BigDecimal.prototype.ONE.subtract( bTotaldiscount.divide(BigDecimal.prototype.HUNDRED,10,BigDecimal.prototype.ROUND_HALF_UP)));
  }

  //checkNumber(frm);
  addDiscountValue();
  if(frm.warehouseid != null && frm.warehouseid.value == "-1")
    frm.warehouseid.disabled = true;
  formatNumberForOrder('');
  frm.pagename.value="salesorderdetailsummary";
  frm.actionname.value="add";
  frm.target="_self";
  isRunning = true;
  //alert(frmToString(frm));
  frm.submit();
}
function page_delete()
{
  if(nullChecked(frm.detailids))
  {
    alert('<%= message.getMessageDesc("MUST_SELECT_ONE_RECORD_TO_DEL") %>');
    return;
  }
  if(!reSumAll()) return;
  addDiscountValue();
  //emptyToNull(frm);
  if(frm.warehouseid != null)
    frm.warehouseid.disabled = true;
 frm.pagename.value="salesorderdetailsummary";
  frm.actionname.value="delete";
  frm.target="_self";
  isRunning = true;
  //alert(frmToString(frm));
  frm.submit();
}
function page_save(opt)
{
 <%if(isServiceOrder){%>
 for(var idx=0;idx< <%=JspHelper.filterNull(orderDetails.size())%>;idx++)
 {
	var linetype=eval("frm.linetype" + idx); 
	var linenetprice = eval("frm.linenetprice" + idx);
	  if(linetype.value=="4" || linetype.value=="5" || linetype.value=="6")
	  {
	  	if(Number(linenetprice.value)!=0)
	   {
	   alert("<%= message.getMessageDesc("VALUE_MUST_BE_ZERO",label.getLabelName("L_NetPrice",false)) %>"+"["+(idx+1)+"]");
   
	  	return;
	   }
	  } 
	} 
  <%}%> 
  
   <!--changeTotalDiscountAmt();-->
  if(!opt && !checkAllQty()) return false;
  if(!sumAll()) return false;
  addDiscountValue();
  if(frm.warehouseid != null)
    frm.warehouseid.disabled = true;

  //emptyToNull(frm);
  frm.pagename.value="salesorderdetailsummary";
  frm.actionname.value="save";
  frm.target="_self";
  isRunning = true;
  //alert(frmToString(frm));
  frm.submit();
}
function page_calprice()
{
  frm.subtotalamt.value = frm.subtotalamt.realsubtotalamt;
  if(frm.totaldiscount.value)
    frm.totalamount.value = frm.subtotalamt.value - frm.totaldiscountamt.value;
  else
  {
    var bSubtotalamt = new BigDecimal("0");
    if (!isEmpty(frm.subtotalamt.value))
        bSubtotalamt = new BigDecimal(""+frm.subtotalamt.value);
    var bTotaldiscount = new BigDecimal("0");
    if (!isEmpty(frm.totaldiscount.value))
        bTotaldiscount = new BigDecimal(""+frm.totaldiscount.value);
    frm.totalamount.value = bSubtotalamt.multiply(BigDecimal.prototype.ONE.subtract( bTotaldiscount.divide(BigDecimal.prototype.HUNDRED,10,BigDecimal.prototype.ROUND_HALF_UP)));
  }
  addDiscountValue();
  //checkNumber(frm);
  if(frm.warehouseid != null)
    frm.warehouseid.disabled = true;
  frm.pagename.value="salesorderdetailsummary";
  frm.actionname.value="calcprice";
  frm.target="_self";
  isRunning = true;
  //alert(frmToString(frm));
  frm.submit();
}

function formatNumberForOrder(index)
{
  var unitprice = eval("frm.unitprice" + index);
  var discount = eval("frm.discount" + index);
  var discountamt = eval("frm.discountamt" + index);
  var linenetprice = eval("frm.linenetprice" + index);
  var locallinenetprice = eval("frm.locallinenetprice" + index);
  var orderqty = eval("frm.orderqty" + index);
  var amount = eval("frm.amount" + index);
  var zctamount = eval("frm.zctamount" + index);
  var taxrate = eval("frm.taxrate" + index);
  var pretaxprice = eval("frm.pretaxprice" + index);
  var taxamount = eval("frm.taxamount" + index);
  var pretaxamount = eval("frm.pretaxamount" + index);
  var baseqty = eval("frm.baseqty" + index);
  unitprice.value = numberRound(numberToString(unitprice.value),unitprice.dataFormat);
  discount.value = numberRound(numberToString(discount.value),discount.dataFormat);
  if(discount.value == "NaN") discount.value = "";
  discountamt.value = numberRound(numberToString(discountamt.value),discountamt.dataFormat);
  if(discountamt.value == "NaN") discountamt.value = "";
  linenetprice.value = numberRound(numberToString(linenetprice.value),linenetprice.dataFormat);
  locallinenetprice.value = numberRound(numberToString(locallinenetprice.value),locallinenetprice.dataFormat);
  orderqty.value = numberRound(numberToString(orderqty.value),orderqty.dataFormat);
  if(orderqty.value == "NaN") orderqty.value = "";
  amount.value = numberRound(numberToString(amount.value),amount.dataFormat);
  zctamount.value = numberRound(numberToString(zctamount.value),zctamount.dataFormat);
  taxrate.value = numberRound(numberToString(taxrate.value),taxrate.dataFormat);
  pretaxprice.value = numberRound(numberToString(pretaxprice.value),pretaxprice.dataFormat);
  taxamount.value = numberRound(numberToString(taxamount.value),taxamount.dataFormat);
  pretaxamount.value = numberRound(numberToString(pretaxamount.value),pretaxamount.dataFormat);
  baseqty.value = numberRound(numberToString(baseqty.value),baseqty.dataFormat);
  formatTotalNumberForOrder();
}
function formatTotalNumberForOrder()
{
  var totalamount = eval("frm.totalamount");
  var totaltaxamt = eval("frm.totaltaxamount");
  var subtotalamt = eval("frm.subtotalamt");
  var totaldiscount = eval("frm.totaldiscount");
  var totaldiscountamt = eval("frm.totaldiscountamt");
  var zcttotalamount = eval("frm.zcttotalamount");
  var zctsubtotalamt = eval("frm.zctsubtotalamt");

  totalamount.value = numberRound(numberToString(totalamount.value),totalamount.dataFormat);
  totaltaxamt.value = numberRound(numberToString(totaltaxamt.value),totaltaxamt.dataFormat);
  subtotalamt.value = numberRound(numberToString(subtotalamt.value),subtotalamt.dataFormat);
  totaldiscount.value = numberRound(numberToString(totaldiscount.value),totaldiscount.dataFormat);
  totaldiscountamt.value = numberRound(numberToString(totaldiscountamt.value),totaldiscountamt.dataFormat);
  zctsubtotalamt.value = numberRound(numberToString(zctsubtotalamt.value),zctsubtotalamt.dataFormat);
  zcttotalamount.value = numberRound(numberToString(zcttotalamount.value),zcttotalamount.dataFormat);
}
function destroy()
{
  frm.itemid.value = "";
  frm.itemcode.value="";
  frm.itemname.value="";
  frm.selleritemcode.value="";
  frm.buyeritemcode.value="";
  frm.selleritemid.value="";
  frm.buyeritemid.value="";
  frm.unitprice.value="";
  frm.listprice.value="";
  frm.taxrate.value="";
  frm.taxamount.value="";
  frm.orderqty.value="";
  frm.discount.value="";
  frm.discountamt.value="";
  frm.amount.value="";
  frm.linenetprice.value="";
  frm.shipfromitemid.value="";
  frm.zctrate.value="";
  frm.locallinenetprice.value="";
  frm.zctamount.value="";
}
